package com.ryan.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.route.entity.TravelAdvisor;
import com.ryan.route.mapper.TravelAdvisorMapper;
import com.ryan.route.service.TravelAdvisorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定制师 服务实现类
 * </p>
 *
 * @author ryan
 * @since 2020-07-24
 */
@Service
public class TravelAdvisorServiceImpl extends ServiceImpl<TravelAdvisorMapper, TravelAdvisor> implements TravelAdvisorService {

    /**
     * 分页+条件查询所有的定制师
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<TravelAdvisor> queryPage(long pageIndex, long pageSize, String name) {
        QueryWrapper<TravelAdvisor> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("gmt_create");

        Page<TravelAdvisor> advisorPage = new Page<>(pageIndex, pageSize);
        baseMapper.selectPage(advisorPage, wrapper);
        return advisorPage;

    }

    /**
     * 获取前台推荐定制师，4位
     * @return
     */
    @Override
    @Cacheable(value = "advisorList", key = "'getIndexAdvisor'")//返回的数据保存到redis做缓存
    public List<TravelAdvisor> getIndexAdvisor() {
        QueryWrapper<TravelAdvisor> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        wrapper.last("limit 4");
        List<TravelAdvisor> advisorList = baseMapper.selectList(wrapper);
        return advisorList;
    }
}
